package com.zhouyipeng.wirte;


import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;


public abstract class TimeTaskWriteFile{

	private int count = 20000;
	
	abstract public String getSqlStatement();
	abstract public String getFilePath();
	abstract public String getFileName();
	abstract public String[] getKeys();
	abstract public String getCountSqlStatement();
	
	public int getCount() {
		return this.count;
	}
	/**
	 * 分页写入文件
	 * @param separator
	 */
	public void exportDate2FileByPageQuery(String separator) {
		String filePath = this.getFilePath();
		
		File file = new File(filePath);
		
		if(!file.exists()) {
			file.mkdirs();
		}
		if(!file.exists()) {
			System.out.println("无法创建文件夹！");
			return;
		}
//		if(file.exists()){//将已有的文件清理掉；防止重复记录
//			File files[] = file.listFiles();
//			System.out.println("==========length=========="+files.length);			
//			if(files.length>0){
//				for(int i=0;i<files.length;i++){
//					File f = files[i];
//					System.out.println("==============fileName=============="+f.getName());
//					f.delete();
//				}
//			}
//		}
		
		//分页写入文件
		int cnt = this.selectTotalConut();
		for (int i = 0; i <= cnt / count; i++) {
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("startRow", i*count);
			params.put("endRow", (i+1)*count);
			this.writeTargetFile(this.pageQueryResult(params),separator, true);
		}
		System.out.println("--------------------"+this.getFileName()+"文件写入了"+cnt+"条数据--------------------");
	}
	
	
	/**
	 * 查询总条数
	 * @return
	 */
	private int selectTotalConut() {
		List<String> strings = new ArrayList<>();
		return strings.size();
	}
	
	/**
	 * 分页查询的结果
	 * @param params
	 * @return
	 */
	private List<Map<String, Object>> pageQueryResult(Map<String, Object> params) {
		List<Map<String, Object>> ret = new ArrayList<>();
		return ret;
	}
	/**
	 * 生成文件
	 * @param result
	 * @param separator
	 * @param append
	 * @return
	 */
	private boolean writeTargetFile(List<Map<String, Object>> result,String separator, boolean append) {
		String filePath = this.getFilePath();
		String fileName = this.getFileName();
		FileLength  fileLength = new FileLength(filePath+"/"+fileName); 
		
		try {
			File file = new File(filePath+"/"+fileName);
			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < result.size(); i++) {
				Map<String, Object> map = result.get(i);
				String keys[] = this.getKeys();
				for(int j=0;j<keys.length;j++){
					Object value = map.get(keys[j]);
					sb.append(separator);
					if (value != null) {
						sb.append(value.toString().trim());
					}
				}
				sb.append(separator);
				sb.append("\n");
			}
			fileLength.write(file, sb.toString().getBytes());
			return true;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return false;
		} finally {
			
		}
	}
	
	/**
	 * 日期-1
	 * */
	public String fmtDateStr(){
		Date date = new Date();
		Long m1 = date.getTime();
		Long m2 = m1-24*60*60*1000;
		Date date2 = new Date(m2);
		String dateStr = new SimpleDateFormat("yyyyMMdd").format(date2);
		return dateStr;
	}
}
